package kuang.demo02;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SaleTicketDemo02 {

    public static void main(String[] args) {
        Ticket2 ticket = new Ticket2();

        new Thread(() -> {
            for(int i = 0; i < 50; i++){
                ticket.sale();
            }
        }, "A").start();

        new Thread(() -> {
            for(int i = 0; i < 50; i++){
                ticket.sale();
            }
        }, "B").start();

        new Thread(() -> {
            for(int i = 0; i < 50; i++){
                ticket.sale();
            }
        }, "C").start();
    }
}

class Ticket2{
    private int number = 50;

    //默认非公平锁
    private Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            if(number > 0){
                System.out.println(Thread.currentThread().getName()
                        + "卖出了第" + (number--) + "张票，还剩" + number + "张票");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
